import java.util.ArrayList;

public class Game {
	
	private static int numberOfPlayers;
	private Player redPlayer;
	private Player greenPlayer;
	private ArrayList <GameObserverInterface> redCarObservers;
	private ArrayList <GameObserverInterface> greenCarObservers;
	
	public Game(Player redPlayer){
		this.redPlayer = redPlayer;
		redCarObservers = new ArrayList <GameObserverInterface> ();
	}
	
	public Game(Player redPlayer, Player greenPlayer){
		this.redPlayer = redPlayer;
		redCarObservers = new ArrayList <GameObserverInterface> ();
		this.greenPlayer = greenPlayer;
		greenCarObservers = new ArrayList <GameObserverInterface> ();
	}
	
	public static void setNumberOfPlayers(int n){
		numberOfPlayers = n;
	}
	
	public static int getNumberOfPlayers(){
		return numberOfPlayers;
	}
	
	public void moveRedCar(String direction){
		redPlayer.setDirection(direction);
		if (direction.equals("Arriba")) {
			redPlayer.setYPossition(redPlayer.getYPossition() - 5);
			this.notifyRedCarObservers();
		} else if (direction.equals("Abajo")) {
			redPlayer.setYPossition(redPlayer.getYPossition() + 5);
			this.notifyRedCarObservers();
		} else if (direction.equals("Izquierda")) {
			redPlayer.setXPossition(redPlayer.getXPossition() - 5);
			this.notifyRedCarObservers();
		} else if (direction.equals("Derecha")) {
			redPlayer.setXPossition(redPlayer.getXPossition() + 5);
			this.notifyRedCarObservers();
		}
	}
	
	public void registerRedCarObserver(GameView view){
		redCarObservers.add(view);
	}
	
	public void notifyRedCarObservers(){
		for(GameObserverInterface aux : redCarObservers) {
			aux.updateRedPossition(redPlayer.getDirection(), redPlayer.getXPossition(), redPlayer.getYPossition());
		}
	}
	
	public void registerGreenCarObserver(GameView view){
		greenCarObservers.add(view);
	}
	
	public void notifyGreenCarObservers(){
		for(GameObserverInterface aux : greenCarObservers) {
			aux.updateRedPossition(greenPlayer.getDirection(), greenPlayer.getXPossition(), greenPlayer.getYPossition());
		}
	}
}
